use sea_orm::ActiveValue::Set;
use sea_orm::{ActiveModelTrait, EntityTrait};

use common::ctx::get_db_by_key;
use common::error::MyError;
use entity::limit_create_character;
use entity::prelude::LimitCreateCharacter;

pub struct LimitCreateCharacterManager {}

impl LimitCreateCharacterManager {
    pub async fn reset_create_role(uid: u32) -> Result<bool, MyError> {
        let entity = LimitCreateCharacter::find_by_id(uid)
            .one(&*get_db_by_key("d_taiwan")?)
            .await?
            .ok_or(MyError::ServerError(format!(
                "limit_create_character [{:?}] does not exist",
                uid
            )))?;
        let mut entity: limit_create_character::ActiveModel = entity.into();
        entity.count = Set(0);
        let _result = entity.update(&*get_db_by_key("d_taiwan")?).await?;
        Ok(true)
    }
}